/*
 * @Author: silence
 * @Date: 2023-07-17 14:29:20
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-08 10:46:40
 * @Description: 商务合作
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/pages/Mine/MinePage/MineController.dart';
import 'package:ems_neutral_app/utils/color/ColorUtils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:sil_utils_package/sil_utils_package.dart';

import '../../../common/module/IdeaFeedbackWidget/IdeaFeedbackWidget.dart';
import '../../../common/widgets/cubitWidget/BottomSheetView.dart';
import '../../../common/widgets/cubitWidget/DividingLine.dart';
import '../../../common/widgets/cubitWidget/SettingItem.dart';
import '../../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../../../config/AppInfo.dart';
import '../../../utils/router/ARouter.dart';
import '../AccountManager/ChooseCountryPage.dart';
import '../AccountManager/Controller/AccountManagerController.dart';
import '../Language/LanguageController.dart';

class BusinessPage extends StatefulWidget {
  const BusinessPage({super.key});

  @override
  State<BusinessPage> createState() => _BusinessPageState();
}

class _BusinessPageState extends State<BusinessPage> {
  AccountManagerController _accountManagerController =
      Get.put(AccountManagerController());
  MineController _mineController = Get.put(MineController());
  String? _countryName;
  String? _countryId;
  String? _phone;
  String? _email;
  String? _detail;
  String? _name;

  /// 区号
  late String _areaCode;
  late List<String> _telCodeList;
  LanguageController _languageController = LanguageController();
  bool _isPromptEnglish = true;

  @override
  void initState() {
    super.initState();
    _areaCode = _accountManagerController.areaCode;
    _telCodeList = kTelCodeList;
    _isPromptEnglish = _languageController.isPromptEnglish();
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      sideMargin: 0,
      title: LangOf(context)?.business?.business,
      body: mainWidget(),
    );
  }

  mainWidget() {
    List<Widget> slivers = [];
    slivers.add(SliverToBoxAdapter(
        child: Container(
      height: 40.w,
    )));

    /// 姓名
    slivers.add(SliverToBoxAdapter(child: inputNameView()));

    /// 选择国家
    slivers.add(SliverToBoxAdapter(child: changeContryView()));

    /// 手机号
    slivers.add(SliverToBoxAdapter(child: phoneView()));

    /// 输入邮箱
    slivers.add(SliverToBoxAdapter(child: emailView()));

    /// 合作详情
    slivers.add(SliverToBoxAdapter(child: detailView()));

    /// 提交
    slivers.add(SliverToBoxAdapter(
      child: SubmitBtn(
        margin: EdgeInsets.only(top: 20.w),
        title: LangOf(context)?.business?.submit,
        onTapFunc: saveSetHandler,
      ),
    ));

    return CustomScrollView(slivers: slivers);
  }

  /// 提交
  saveSetHandler() {
    _mineController.requestCompanyIcooperate(
        context: context,
        name: _name,
        countryId: _countryId,
        email: _email,
        phone: _phone,
        areaCode: _areaCode,
        cooperateContent: _detail,
        successHandler: (p0) {
          ARoute.pop(context);
        });
  }

  /// 输入姓名
  inputNameView() {
    return SettingItem(
      margin: EdgeInsets.symmetric(vertical: 5.w, horizontal: 20.w),
      title: LangOf(context)?.business?.name,
      leftImgPath: 'imgs/common/bt.png',
      leftImgHeight: 12.w,
      leftImgWidth: 12.w,
      dividingLineHide: true,
      rightView: Container(
        margin: EdgeInsets.only(left: 50.w),
        child: SilTextField(
          hintText: LangOf(context)?.business?.namePhane,
          onValid: (valid, value) {
            _name = value;
          },
          interface: SilFormattersNickName(),
        ),
      ),
      onTapFunc: () {},
    );
  }

  /// 选择国家
  changeContryView() {
    return SettingItem(
        margin: EdgeInsets.symmetric(vertical: 5.w, horizontal: 20.w),
        title: LangOf(context)?.business?.country,
        leftImgPath: 'imgs/common/bt.png',
        leftImgHeight: 12.w,
        leftImgWidth: 12.w,
        dividingLineHide: true,
        rightView: Container(
          margin: EdgeInsets.only(left: 50.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(
                  child: SilText(
                LangOf(context)?.business?.language,
                fontSize: 14.sp,
                color: SilTools.stringIsEmpty(_countryName)
                    ? ColorUtils.color_subtitle
                    : ColorUtils.color_font_main,
              )),
              Container(
                  margin: EdgeInsets.only(left: 10.w),
                  width: 24.w,
                  height: 24.w,
                  child: SilImage('imgs/personal/list_icon_arrow@3x.png'))
            ],
          ),
        ),
        onTapFunc: () =>
            ARoute.gotoPage(context, ChooseCountryPage()).then((value) {
              if (value == null) {
                return;
              }

              if (!SilTools.stringIsEmpty(value.cnCountry) &&
                  !SilTools.stringIsEmpty(value.enCountry)) {
                _countryName =
                    _isPromptEnglish ? value.enCountry : value.cnCountry;
                _countryId = value.id;
                setState(() {});
              }
            }));
  }

  /// 手机号
  phoneView() {
    return SettingItem(
      margin: EdgeInsets.symmetric(vertical: 5.w, horizontal: 20.w),
      title: LangOf(context)?.business?.phone,
      dividingLineHide: true,
      rightView: Container(
        alignment: Alignment.center,
        child: Container(
            margin: EdgeInsets.only(left: 50.w),
            child: Row(
              children: [
                SilButton(
                  imgPath: 'imgs/common/common_sanjx.png',
                  imgHeight: 15.w,
                  imgWidth: 15.w,
                  title: _areaCode,
                  titleColor: ColorUtils.color_font_main,
                  fontSize: 14.sp,
                  margin: EdgeInsets.only(right: 10.w),
                  onTapFunc: showTelCodeListPop,
                ),
                Expanded(
                    child: SilTextField(
                  hintText: LangOf(context)?.business?.phonePhane,
                  onValid: (valid, value) {
                    _phone = value;
                  },
                  interface: SilFormattersPhone(),
                )),
              ],
            )),
      ),
    );
  }

  showTelCodeListPop() {
    SilBottomSheet.show(
        context: context,
        child: BottomSheetView(
          titles: _telCodeList,
          initialSelectedItem: _telCodeList.indexOf(_areaCode),
          tapSureSeletedFunc: (selected) {
            _areaCode = _telCodeList[selected];
            setState(() {});
          },
        ));
  }

  /// 输入邮箱
  emailView() {
    return SettingItem(
      margin: EdgeInsets.symmetric(vertical: 5.w, horizontal: 20.w),
      title: LangOf(context)?.business?.email,
      leftImgPath: 'imgs/common/bt.png',
      leftImgHeight: 12.w,
      leftImgWidth: 12.w,
      dividingLineHide: true,
      rightView: Container(
        margin: EdgeInsets.only(left: 50.w),
        child: SilTextField(
          hintText: LangOf(context)?.business?.emailPhane,
          onValid: (valid, value) {
            _email = value;
          },
          interface: SilFormattersNickName(),
        ),
      ),
    );
  }

  /// 合作详情
  detailView() {
    return Column(
      children: [
        SettingItem(
          margin: EdgeInsets.symmetric(vertical: 5.w, horizontal: 20.w),
          title: LangOf(context)?.business?.details,
          leftImgPath: 'imgs/common/bt.png',
          leftImgHeight: 12.w,
          leftImgWidth: 12.w,
          dividingLineHide: true,
        ),
        Container(
          height: 140.w,
          margin: EdgeInsets.symmetric(horizontal: 20.w),
          child: SilTextInput(
            hint: LangOf(context)?.business?.desc_Phlace,
            maxLength: MaxNumOfFeedback,
            onChanged: (str) {
              _detail = str;
            },
          ),
        ),
        DividingLine(margin: EdgeInsets.symmetric(horizontal: 20.w))
      ],
    );
  }
}
